package vip.xiaonuo.smzq.modular.dictionary.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.smzq.core.gson.GsonUtil;
import vip.xiaonuo.smzq.core.tools.excel.ExcelImportUtil;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb11Kwj;
import vip.xiaonuo.smzq.modular.dictionary.entity.excelEnt.Cb11KwjExc;
import vip.xiaonuo.smzq.modular.dictionary.service.ICb11KwjService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


/**
 * 矿务局
 *
 * @author eomer
 * @since 2024-02-21
 */
@Api(tags = "矿务局")
@RestController
@RequestMapping("/dictionary/cb11Kwj")
@Validated
public class Cb11KwjController {

    @Resource
    private ICb11KwjService iCb11KwjService;

    /**
     * 添加矿务局
     */
    @ApiOperation("添加矿务局")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody @Valid Cb11Kwj cb11Kwj) {
        iCb11KwjService.save(cb11Kwj);
        return CommonResult.ok();
    }

    /**
     * 删除矿务局
     */
    @ApiOperation("删除矿务局")
    @PostMapping("/delete")
    public CommonResult<String> delete(@ApiParam(name = "pkid", value = "主键") @RequestParam String pkid) {
        iCb11KwjService.removeById(pkid);
        return CommonResult.ok();
    }

    /**
     * 编辑矿务局
     */
    @ApiOperation("编辑矿务局")
    @PostMapping("/edit")
    public CommonResult<String> edit(@RequestBody @Valid Cb11Kwj cb11Kwj) {
        iCb11KwjService.updateById(cb11Kwj);
        return CommonResult.ok();
    }

    /**
     * 矿务局分页查询
     */
    @ApiOperation("矿务局分页查询")
    @GetMapping("/list")
    public CommonResult<Page<Cb11Kwj>> page() {
        return CommonResult.data(iCb11KwjService.page(new Cb11Kwj()));
    }

    /**
     * 导入
     */
    @ApiOperation("导入")
    @PostMapping("/import")
    public CommonResult<String> importExcel(@RequestParam("file") MultipartFile file) {
        try {
            List<Cb11KwjExc> list = ExcelImportUtil.readMultipartFile(file, Cb11KwjExc.class);
            List<Cb11Kwj> cb01Zhs = GsonUtil.ObjectToList(list, Cb11Kwj.class);
            iCb11KwjService.saveBatch(cb01Zhs);
            return CommonResult.ok();
        } catch (Exception e) {
            return CommonResult.error(e.getMessage());
        }
    }
}
